<?php
namespace app\index\controller;

use app\index\model\SmsModel;
use app\index\util\Response;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Cache;
use think\Config;
use think\Request;

class Util extends Base
{
    public function sms(){
        $mobile = Request::instance()->request('mobile');
        $time = Cache::get('login'.$mobile);
        if($time){
            Response::json(-2,'请勿频繁发送短信');
        }
        $model = new SmsModel();
        $code = rand(1000,9999);
        \Sms::sendSms($mobile,$code);
        $time = time();
        $data = [];
        $data['code'] = $code;
        $data['mobile'] = $mobile;
        $data['add_time'] = $time;
        $data['status'] = 1;
        $data['expire_time'] = $time + 60 * 30;//30分钟有效
        if($model->insert($data)){
            Cache::set('login'.$mobile,1,60);
            Response::json(1,'短信发送成功',[$time=>60]);
        }
    }
    public function upload(){
        $file = Request::instance()->file('image');
        $accesskey = Config::get('site.qiniu_accesskey');
        $secretKey = Config::get('site.qiniu_secretkey');
        $auth = new Auth($accesskey,$secretKey);
        $bucket = Config::get('site.qiniu_name');
        $token = $auth->uploadToken($bucket);
        $uploadMgr = new UploadManager();
        $result = $uploadMgr->putFile($token,null,$file->getPathname());
        if(isset($result[0]['key'])){
            $data = [];
            $data['status'] = 1;
            $data['url'] = Config::get('site.qiniu_domin').'/'.$result[0]['key'];
        }else{
            $data = [];
            $data['status'] = 0;
            $data['msg'] = 'error';
        }
        $result = json_encode($data);
        echo $result;
        exit();
    }
}
